package org.itydl.game.model

import org.itheima.kotlin.game.core.Painter
import org.itydl.game.Config
import org.itydl.game.business.Attackable
import org.itydl.game.business.Blockable
import org.itydl.game.business.Destoryable
import org.itydl.game.business.Sufferable

/**
 * 大本营
 */
class Camp(override var x: Int, override var y: Int) :
        Blockable, Sufferable, Destoryable { // 阻挡能力，遭受打击能力,自动销毁的能力

    override var width: Int = Config.block * 2
    override var height: Int = Config.block + Config.block / 2

    /**
     * 大本营的外围生命值
     */
    override var blood: Int = 12

    override fun draw() {

        if (blood <= 3) {
            // 外围消失
            x = (Config.gameWidth - Config.block) / 2
            y = Config.gameHeight - Config.block
            width = Config.block
            height = Config.block
            Painter.drawImage("img/symbol.gif", x, y)
        } else if (blood <= 6) {
            Painter.drawImage("img/wall_small.gif", x, y)
            Painter.drawImage("img/wall_small.gif", x + 32, y)
            Painter.drawImage("img/wall_small.gif", x + 64, y)
            Painter.drawImage("img/wall_small.gif", x + 96, y)

            Painter.drawImage("img/wall_small.gif", x, y + 32)
            Painter.drawImage("img/wall_small.gif", x, y + 64)

            Painter.drawImage("img/wall_small.gif", x + 96, y + 32)
            Painter.drawImage("img/wall_small.gif", x + 96, y + 64)

            Painter.drawImage("img/symbol.gif", x + 32, y + 32)
        } else {
            Painter.drawImage("img/steel_small.gif", x, y)
            Painter.drawImage("img/steel_small.gif", x + 32, y)
            Painter.drawImage("img/steel_small.gif", x + 64, y)
            Painter.drawImage("img/steel_small.gif", x + 96, y)

            Painter.drawImage("img/steel_small.gif", x, y + 32)
            Painter.drawImage("img/steel_small.gif", x, y + 64)

            Painter.drawImage("img/steel_small.gif", x + 96, y + 32)
            Painter.drawImage("img/steel_small.gif", x + 96, y + 64)

            Painter.drawImage("img/symbol.gif", x + 32, y + 32)
        }
    }

    /**
     * 遭受攻击的返回修改为集合形式，表示遭受攻击后多个效果
     */
    override fun notifySuffered(attackable: Attackable): Array<View>? {
        blood -= attackable.power
        // 当血量为6的时候，给出一个爆炸效果
        if (blood == 6 || blood == 3) {
            val x = x - 32
            val y = y - 32
            return arrayOf(
                    // 一下课画图理解
                    Blast(x, y),
                    Blast(x + 32, y),
                    Blast(x + Config.block, y),
                    Blast(x + Config.block + 32, y),
                    Blast(x + Config.block * 2, y),
                    Blast(x, y + 32),
                    Blast(x, y + Config.block),
                    Blast(x, y + 32 + Config.block),
                    Blast(x + Config.block * 2, y + 32),
                    Blast(x + Config.block * 2, y + Config.block),
                    Blast(x + Config.block * 2, y + Config.block + 32)
            )
        }
        return null
    }

    override fun isDestory(): Boolean {
        return blood <= 0
    }

    override fun showDestory(): Array<View>? {
        // 展示销毁爆炸效果
        return arrayOf(
                // 第一行
                Blast(x - 32, y - 32),
                Blast(x, y - 32),
                Blast(x + 32, y - 32),

                // 第二行
                Blast(x - 32, y),
                Blast(x, y),
                Blast(x + 32, y),

                // 第三行
                Blast(x - 32, y + 32),
                Blast(x, y + 32),
                Blast(x + 32, y + 32)
        )
    }
}